﻿<MButton Absolute
         Class="@($"m-btn--copy {Class}")"
         Icon
         Right
         Style="top: 14px;"
         Show="Show"
         OnClick="Copy">
    <FadeTransition LeaveAbsolute>
        <ShowTransitionElement Value="_copying">
            <MIcon Color="success" Small>
                mdi-check
            </MIcon>
        </ShowTransitionElement>
        <ShowTransitionElement Value="!_copying">
            <MIcon Color="white" Small>
                mdi-content-copy
            </MIcon>
        </ShowTransitionElement>
    </FadeTransition>
</MButton>

@code {

    [Inject]
    public IJSRuntime Js { get; set; } = null!;

    [Parameter]
    public bool Show { get; set; }

    [Parameter]
    public string? Class { get; set; }

    [Parameter]
    [EditorRequired]
    public string? Content { get; set; }

    private bool _copying;

    private async Task Copy()
    {
        // TODO: HideOnLeave not implemented, can i use LeaveAbsolute?

        if (_copying) return;

        _copying = true;

        await Js.InvokeVoidAsync(JsInteropConstants.Copy, Content);

        await Task.Delay(1500);

        _copying = false;
    }

}